package com.app.admin.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.R;
import com.app.admin.Detail;
import com.app.bean.UserBean;
import com.app.util.AdapterUtil;
import com.app.util.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class PersonFragment extends Fragment {

    private RecyclerView recy;
    private List<UserBean> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person3, container, false);
        initView(view);
        getData();
        setAdapter();
        return view;
    }

    private void setAdapter() {
        AdapterUtil<UserBean> adapter = new AdapterUtil<>(R.layout.user_item, ((data, position, holder) -> {
            TextView text1 = holder.getView(R.id.text1);
            TextView text2 = holder.getView(R.id.text2);
            LinearLayout layout = holder.getView(R.id.layout);
            if (!TextUtils.isEmpty(data.getUser_id()) && data.getUser_id().substring(0, 1).equals("j")) {
                text1.setText("教师:" + data.getUsername());
                text2.setText("职工号:" + data.getUser_id());
            } else {
                text1.setText("学生:" + data.getUsername());
                text2.setText("学号:" + data.getUser_id());
            }
            layout.setOnClickListener(v -> {
                Intent intent = new Intent(getContext(), Detail.class);
                intent.putExtra("id", data.getId());
                startActivity(intent);
            });
        }));
        adapter.listUpdate(list);
        recy.setAdapter(adapter);
    }

    private void getData() {
        list = new ArrayList<>();
        list.clear();
        DBHelper dbHelper = new DBHelper(getContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sqlQuery = "SELECT * FROM user";
        Cursor cursor = db.rawQuery(sqlQuery, null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String username = cursor.getString(cursor.getColumnIndex("username"));
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            String phone_number = cursor.getString(cursor.getColumnIndex("phone_number"));
            String id_card = cursor.getString(cursor.getColumnIndex("id_card"));
            String gender = cursor.getString(cursor.getColumnIndex("gender"));
            UserBean userBean = new UserBean(
                    id, username, user_id, password, phone_number, id_card, gender
            );
            list.add(userBean);
        }
        dbHelper.close();
        cursor.close();
    }

    private void initView(View view) {
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setVisibility(View.GONE);
        ((TextView) toolbar.getChildAt(1)).setText("用户信息");
        recy = view.findViewById(R.id.recy);
        recy.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
        setAdapter();
    }
}